import React, { Component } from 'react'
import { Link } from 'react-router-dom'

import Header from '../components/Index/Header'
import SideBar from '../components/Index/SideBar'

import{NAV} from '../config/config'

import Loginservice from '../services/login'
const loginservice = new Loginservice()

export default class IndexPage extends Component {
    constructor(props){
        super(props)
        this.state={
            curIdx:0,
            field:NAV[0].field,
            title:NAV[0].title
        }
        
    }
    onNavItemClick(dataItem,index){
        this.setState({
            field,
            curIdx:index
        })
    }
    async loginCheck(){
        const result = await loginservice.loginCheck();
        const errorCode = result.error_code;
        if(errorCode === 10006){
            const {history} = this.props;
            history.push('/login')
        }
    }
    componentDidMount(){
        this.loginCheck();
    }
    render() {
        const {history} = this.props,
              {curIdx} = this.state
        return (
            <div className="container">
              <Header history={history}/>
              <SideBar
                curIdx={curIdx}
                onNavItemClick={this.onNavItemClick.bind(this)}
              ></SideBar>
            </div>
        )
    }
}
